package teamgodeater.hicarnet.Fragment;

import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import teamgodeater.hicarnet.Data.UserInfoData;
import teamgodeater.hicarnet.Help.RestClientHelp;
import teamgodeater.hicarnet.Help.UserDataHelp;
import teamgodeater.hicarnet.Layout.LoadingView.RotateLoadingViewHelp;
import teamgodeater.hicarnet.MVP.Base.BaseFragment;
import teamgodeater.hicarnet.MVP.Base.BaseFragmentManage;
import teamgodeater.hicarnet.MVP.Base.ViewTranslateHelp;
import teamgodeater.hicarnet.MVP.Base.WindowsParams;
import teamgodeater.hicarnet.MVP.Ui.Main.MainFragment;
import teamgodeater.hicarnet.R;
import teamgodeater.hicarnet.RestClient.RestClient;
import teamgodeater.hicarnet.Utils.Utils;
import teamgodeater.hicarnet.Widget.RippleBackGroundView;

/**
 * Created by G on 2016/6/24 0024.
 */

public class UserInfoSetFragment extends BaseFragment {
    @Bind(R.id.nikename)
    EditText nikename;
    @Bind(R.id.tel)
    EditText tel;
    @Bind(R.id.email)
    EditText email;
    @Bind(R.id.gender)
    NiceSpinner gender;
    @Bind(R.id.Ok)
    RippleBackGroundView Ok;
    private RotateLoadingViewHelp rotateLoadingViewHelp;

    @NonNull
    @Override
    protected WindowsParams onCreateSupportViewParams(WindowsParams windowsParams) {
        windowsParams.rootLayoutId = R.layout.frgm_user_info_set;
        windowsParams.primaryColor = Utils.getColorFromRes(R.color.colorAccent);
        return windowsParams;
    }

    @Override
    public String getType() {
        return "";
    }

    @Override
    public void onOnceGlobalLayoutListen() {
        ViewTranslateHelp.bottom2Up(mTopView);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, mRootContain);
        tSetDefaultView(true, "设置信息");
        rotateLoadingViewHelp = new RotateLoadingViewHelp(mRootContain);
        setColor();
        setView();
        setListener();
        return rootView;
    }

    private void setListener() {
        Ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RestClientHelp restClientHelp = new RestClientHelp();
                final UserInfoData userInfoData = new UserInfoData();
                userInfoData.setUser_id(-1);
                userInfoData.setEmail(email.getText().toString());
                userInfoData.setGender(gender.getText().toString());
                userInfoData.setPhone(tel.getText().toString());
                userInfoData.setName(nikename.getText().toString());

                rotateLoadingViewHelp.startAnimator("正在更新用户信息 请稍后...");
                restClientHelp.setUserInfo(userInfoData, new RestClient.OnResultListener<String>() {
                    @Override
                    public void succeed(String bean) {
                        rotateLoadingViewHelp.stopAnimator();
                        UserDataHelp.userInfoData = userInfoData;
                        Utils.toast("信息保存成功");
                        finish();
                    }
                    @Override
                    public void error(int code) {
                        rotateLoadingViewHelp.stopAnimator();
                        RestClientHelp.generalErrorToast(code);
                    }
                });
            }
        });
    }

    private void finish() {
        BaseFragmentManage.switchFragment(MainFragment.getInstans());
    }

    private void setView() {
        List<String> dataset = new ArrayList<>();
        dataset.add("保密");
        dataset.add("男");
        dataset.add("女");
        gender.attachDataSource(dataset);
        gender.setSelectedIndex(0);
    }

    private void setColor() {
        nikename.getCompoundDrawables()[0].setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
        email.getCompoundDrawables()[0].setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
        tel.getCompoundDrawables()[0].setColorFilter(getResources().getColor(R.color.colorBlack54), PorterDuff.Mode.SRC_IN);
    }

    @Override
    public void onToolBarClick(View v,String tag) {
        if (tag.equals("back")) {
            finish();
        }
    }

    @Override
    public boolean onInterceptBack() {
        finish();
        return true;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
